package com.woniuxy.woniuhis.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.ExaminationItem;
import com.woniuxy.woniuhis.exception.ExaminationItemServiceException;
import com.woniuxy.woniuhis.service.ExaminationItemService;
import com.woniuxy.woniuhis.util.ResponseJson;

@Controller
@RequestMapping("/examinationitem")
public class ExaminationItemController {
	
	@Autowired
	ExaminationItemService examinationItemService;
	
	/**
	 * 	插入检查条目
	 */
	@RequestMapping("/add")
	@ResponseBody
	public ResponseJson addExaminationItem(@RequestBody ExaminationItem exItem) {
		ResponseJson responseJson = new ResponseJson();
		try {
			if(exItem == null || exItem.getExaminationId() == null || "".equals(exItem.getExaminationId()) ||
					exItem.getExamNameId() == null || "".equals(exItem.getExamNameId()) ||
					exItem.getAmount() == null
					) {
				throw new RuntimeException("插入数据不正确");
			}
			examinationItemService.addExaminationItem(exItem);
			responseJson.setMsg("添加成功");
			responseJson.setCode(0);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}
	/**
	 * 	根据检查条目id更新检查条目表的检查状态
	 */
	@RequestMapping("/updatestatus")
	@ResponseBody
	public ResponseJson modifyExaminationItemById(Boolean examStatus, String examItemId) {
		ResponseJson responseJson = new ResponseJson();
		try {
			if(examItemId == null || "".equals(examItemId)) {
				throw new RuntimeException("检查条目编号不能为空");
			}
			ExaminationItem exam = examinationItemService.findExaminationItemById(examItemId);
			if(exam == null) {
				throw new RuntimeException("检查条目编号不存在");
			}
			examinationItemService.modifyExaminationItemById(examStatus, examItemId);
			responseJson.setMsg("修改成功");
			responseJson.setCode(0);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
		
	}
	/**
	 * 	查询指定检查条目的信息,根据id
	 */
	@RequestMapping("/item")
	@ResponseBody
	public ResponseJson findExaminationItemById(String examItemId) {
		ResponseJson responseJson = new ResponseJson();
		try {
			if(examItemId == null || "".equals(examItemId)) {
				throw new RuntimeException("检查条目编号不能为空");
			}
			ExaminationItem examinationItem = examinationItemService.findExaminationItemById(examItemId);
			if(examinationItem == null) {
				throw new RuntimeException("检查条目编号不存在");
			}
			List<ExaminationItem> list = new ArrayList<ExaminationItem>();
			list.add(examinationItem);
			responseJson.setData(list);
			responseJson.setCode(0);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}
	
	/**
	 *  根据检查单号查询所有的检查条目
	 */
	@RequestMapping("/examination")
	@ResponseBody
	public ResponseJson findAllExaminationItemsByExaminationId(String examinationId) {
		ResponseJson responseJson = new ResponseJson();
		try {
			if(examinationId == null || "".equals(examinationId)) {
				throw new RuntimeException("检查单编号不能为空");
			}
			List<ExaminationItem> examinationItems = examinationItemService.findAllExaminationItemsByExaminationId(examinationId);
			if(examinationItems == null ||examinationItems.size() == 0) {
				throw new RuntimeException("检查单编号可能不存在");
			}
			responseJson.setData(examinationItems);
			responseJson.setCode(0);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}
	
	/**
	 *	条件查询，或者全部查询
	 */
	@RequestMapping("/all")
	@ResponseBody
	public ResponseJson<ExaminationItem> findAllExaminationItems(@RequestBody ExaminationItem item) {
		ResponseJson<ExaminationItem> responseJson = new ResponseJson<ExaminationItem>();
		try {
			// 数据验证
			Integer page = item.getPage();
			Integer limit = item.getLimit();
			if(page == null) {
				page = 1;
			}
			if(limit == null) {
				limit = 10;
			}
			PageInfo<ExaminationItem> examinationItems = examinationItemService.findAllExaminationItems(item,page,limit);
			responseJson.setData(examinationItems.getList());
			responseJson.setCount((int)examinationItems.getTotal());
			responseJson.setCode(0);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}
	
}

